//using System;
//using System.Collections.Generic;
//using System.Drawing;
//using System.IO;
//using System.Linq;
//using System.Windows.Forms;
//using Firestarter.DB.Schema.BO;
//using FireStarterModeller.BO;
//using FireStarterModeller.Logic;
//
//using Habanero.Base;
//using Habanero.Base.Exceptions;
//using Habanero.BO;
//using Habanero.BO.Loaders;
//using Habanero.Faces.Base;
//using Habanero.Faces.Win;
//using Microsoft.Practices.Unity;
//using DockStyle=DockStyle;
//
//namespace FireStarterModeller.UI
//{
//    public class StartupSolutionControl : UserControlWin, IFormControl
//    {
//        public IControlFactory ControlFactory { get; private set; }
//        private IFormHabanero _solutionControlForm;
//        private IFormHabanero _selectSolutionControlForm;
//        private DMSolution _dmSolution;
//        private DataStoreInMemory _dataStoreInMemory;
//        private RecentProjects _recentProjects;
//        private FileSystemWatcher _fileWatchClassDefs;
//        public event EventHandler<BOEventArgs<DMSolution>> SolutionSaved;
//
//
//        public StartupSolutionControl()
//        {
//            this.ControlFactory = GlobalUIRegistry.ControlFactory;
//            IPanel headingPanel = this.ControlFactory.CreatePanel();
//            GroupBoxWin newOpenGroupBox = CreateNewOpenControl();
//            //IPanel recentSolutionPanel = CreateRecentSolutionPanel();
//            headingPanel.Width = 500;
//            headingPanel.Height = 700;
//            IPanel mainPanel = this.ControlFactory.CreatePanel();
//            mainPanel.Width = 1100;
//            mainPanel.Height = 700;
//            this.BackColor = Color.White;
////            ColumnLayoutManager mainPanelLayoutManager = new ColumnLayoutManager(mainPanel, this.ControlFactory);
////            mainPanelLayoutManager.ColumnCount = 1;
//            ColumnLayoutManager mainPanelLayoutManager = new ColumnLayoutManager(mainPanel, this.ControlFactory) { ColumnCount = 1 };
//            BorderLayoutManagerWin borderLayoutManagerWin = new BorderLayoutManagerWin(headingPanel, this.ControlFactory);
//            borderLayoutManagerWin.AddControl(newOpenGroupBox, BorderLayoutManager.Position.Centre);
//            mainPanelLayoutManager.AddControl(headingPanel);
////            mainPanelLayoutManager.AddControl(newOpenGroupBox);
//            //mainPanelLayoutManager.AddControl(recentSolutionPanel);
//            BorderLayoutManager layoutManager = this.ControlFactory.CreateBorderLayoutManager(this);
//            layoutManager.AddControl(mainPanel, BorderLayoutManager.Position.Centre);
////            layoutManager.AddControl(mainPanel);
//        }
//
////        private PanelWin CreateRecentSolutionPanel()
////        {
////            PanelWin panel = new PanelWin { Height = 200, Width = 500 }; 
////            BusinessObjectCollection<RecentSolution> collection = BORegistry.DataAccessor.BusinessObjectLoader.GetBusinessObjectCollection<RecentSolution>("");
////            LabelWin labelWin = new LabelWin();
////           
////
////            if (collection.Count > 0)
////            {
////                labelWin.Text = "Recently Opened Solutions...";
////                labelWin.Width = 200;
////                labelWin.Font = new System.Drawing.Font("Arial", 9.75F,FontStyle.Bold);
////                labelWin.ForeColor = Color.DarkOrange;
////                panel.Controls.Add(labelWin);
////                _recentSolution = collection[0];
////                DMSolution solution1 = _recentSolution.Solution1;
////                DMSolution solution2 = _recentSolution.Solution2;
////                DMSolution solution3 = _recentSolution.Solution3;
////                int topPosition = 25;
////                if (solution1 != null && !solution1.Status.IsDeleted)
////                {
////                    LinkLabel solution1Label = CreateSolutionLabel(solution1, topPosition);
////                   
////                    topPosition += 30;
////                    panel.Controls.Add(solution1Label);
////                }
////                if (solution2 != null && !solution2.Status.IsDeleted)
////                {
////                    LinkLabel solution2Label = CreateSolutionLabel(solution2, topPosition);
////                    topPosition += 30;
////                    panel.Controls.Add(solution2Label);
////                }
////                if (solution3 != null && !solution3.Status.IsDeleted)
////                {
////                    LinkLabel solution3Label = CreateSolutionLabel(solution3, topPosition);
////                    panel.Controls.Add(solution3Label);
////                }
////            }
////            return panel;
////        } 
//        
//        
//         private Panel CreateRecentSolutionPanel()
//         {
//            Panel panel = new Panel { Height = 200, Width = 500 }; 
////          BusinessObjectCollection<RecentSolution> collection = BORegistry.DataAccessor.BusinessObjectLoader.GetBusinessObjectCollection<RecentSolution>("");
//            List<string> projectsList = RecentProjects.ProjectsList;
//            LabelWin labelWin = new LabelWin();
//
//            labelWin.Text = "Recently Opened Solutions...";
//            labelWin.Top = 20;
//            labelWin.Width = 200;
//            labelWin.Font = new System.Drawing.Font("Arial", 9.75F, FontStyle.Bold);
//            labelWin.ForeColor = Color.DarkOrange;
//            panel.Controls.Add(labelWin);
//            int topPos = 40;
//            foreach (string project in projectsList)
//            {
//                string display = ShortenPathToLength(project, 40);
//                LinkLabel label = CreateSolutionLabel(display, topPos);
//                panel.Controls.Add(label);
//                topPos += 20;
//            }
//            return panel;
//        }
//
//
//        private LinkLabel CreateSolutionLabel(string solutionName, int topPosition)
//        {
//            LinkLabel solutionLabel = new LinkLabel();
//            solutionLabel.Text = solutionName;
//            solutionLabel.Top = topPosition;
//            solutionLabel.Width = 300;
//            solutionLabel.Left = 5;
//            solutionLabel.Font = new System.Drawing.Font("Arial", 9.75F);
//            solutionLabel.ForeColor = Color.Blue;
//            solutionLabel.Click += RecentItem_Click;
////            solutionLabel.Click += delegate
////            {
////                if (this.SolutionSaved != null)
////                {
////                    try
////                    {
////                        this.SolutionSaved(this, new BOEventArgs<DMSolution>(solution));
////
////                    }
////                    catch (Exception ex)
////                    {
////                        GlobalRegistry.UIExceptionNotifier.Notify(ex, "", "Error ");
////                    }
////                }
////            };
//            return solutionLabel;
//        }
//
//        #region Recent Files List
//
//        private void LoadProjectList()
//        {
//            List<string> projectsList = RecentProjects.ProjectsList;
//            //            ToolStripItemCollection items = recentFilesToolStripMenuItem.DropDownItems;
//            //            foreach (ToolStripItem toolStripItem in items)
//            //            {
//            //                toolStripItem.Click -= RecentItem_Click;
//            //            }
//            //            items.Clear();
//            //            foreach (string project in projectsList)
//            //            {
//            //                string display = ShortenPathToLength(project, 40);
//            //                ToolStripItem item = items.Add(display);
//            //                item.ToolTipText = project;
//            //                item.Tag = project;
//            //            }
//            //            foreach (ToolStripItem toolStripItem in items)
//            //            {
//            //                toolStripItem.Click += RecentItem_Click;
//            //            }
////            UpdateState();
//        }
//
//        private static string ShortenPathToLength(string path, int limit)
//        {
//            if (path.Length > limit)
//            {
//                string drive = path.Substring(0, 3);
//                string filePath = path.Substring(3);
//                int pos = filePath.Length - (limit - 3);
//                filePath = filePath.Substring(pos);
//                pos = filePath.IndexOf(@"\");
//                if (pos >= 0)
//                {
//                    filePath = filePath.Substring(pos);
//                }
//                return drive + "..." + filePath;
//            }
//            else
//            {
//                return path;
//            }
//        }
//
//        private void RecentItem_Click(object sender, EventArgs e)
//        {
//            
//            LinkLabel linkLabel = (LinkLabel) sender;
//            MessageBox.Show(linkLabel.Text);
//            OpenProject(linkLabel.Text);
//        }
//
//     
//        private void UpdateRecentSolution()
//        {
//            //            if (_dmSolution == null) return;
//            //            BusinessObjectCollection<RecentSolution> collection =
//            //                BORegistry.DataAccessor.BusinessObjectLoader.GetBusinessObjectCollection<RecentSolution>("");
//            //            RecentSolution recentSolution;
//            //            if (collection.Count > 0)
//            //            {
//            //                recentSolution = collection[0];
//            //                DMSolution solution1 = recentSolution.Solution1;
//            //                DMSolution solution2 = recentSolution.Solution2;
//            //                DMSolution solution3 = recentSolution.Solution3;
//            //                if (_dmSolution == solution1 || _dmSolution == solution2 || _dmSolution == solution3) return;
//            //                recentSolution.Solution3 = solution2;
//            //                recentSolution.Solution2 = solution1;
//            //                recentSolution.Solution1 = _dmSolution;
//            //                recentSolution.Save();
//            //            }
//            //            else
//            //            {
//            //                recentSolution = new RecentSolution { Solution1 = _dmSolution };
//            //                recentSolution.Save();
//            //            }
//        }
//
//   
//        private RecentProjects RecentProjects
//        {
//            get
//            {
//                if (_recentProjects == null)
//                {
//                    _recentProjects = new RecentProjects(Path.Combine(Application.StartupPath, "projects.xml"));
//                }
//                return _recentProjects;
//            }
//        }
//
//        private void OpenLinkLabel_OnClick(object sender, EventArgs e)
//        {
//            try
//            {
//                SelectSolutionControl selectSolutionControl = new SelectSolutionControl();
//                _selectSolutionControlForm = this.ControlFactory.CreateForm();
//                _selectSolutionControlForm.Controls.Add(selectSolutionControl);
//                selectSolutionControl.Dock = DockStyle.Fill;
//                selectSolutionControl.SolutionSelected += SelectSolutionControl_OnSolutionSelected;
//                _selectSolutionControlForm.Width = 400;
//                _selectSolutionControlForm.Height = 300;
//                _selectSolutionControlForm.ShowDialog();
//            }
//            catch (Exception ex)
//            {
//                GlobalRegistry.UIExceptionNotifier.Notify(ex, "", "Error ");
//            }
//        }
//
//        private void SelectSolutionControl_OnSolutionSelected(object sender, BOEventArgs<DMSolution> e)
//        {
//            try
//            {
//                DMSolution dmSolution = e.BusinessObject;
//                _selectSolutionControlForm.Close();
//                if (this.SolutionSaved != null)
//                {
//                    this.SolutionSaved(this, new BOEventArgs<DMSolution>(dmSolution));
//                }
//            }
//            catch (Exception ex)
//            {
//                GlobalRegistry.UIExceptionNotifier.Notify(ex, "", "Error ");
//            }
//        }
//
//
//        private void NewLinkLabel_OnClick(object sender, EventArgs e)
//        {
//            try
//            {
//                CreateSolutionControl createSolutionControl  = new CreateSolutionControl();
//                _solutionControlForm = this.ControlFactory.CreateForm();
//                _solutionControlForm.Controls.Add(createSolutionControl);
//                createSolutionControl.Dock = DockStyle.Fill;
//                createSolutionControl.SolutionSaved+=SolutionControl_OnSolutionSaved;
//                _solutionControlForm.Width = 400;
//                _solutionControlForm.Height = 300;
//                _solutionControlForm.ShowDialog();
//            }
//            catch (Exception ex)
//            {
//                GlobalRegistry.UIExceptionNotifier.Notify(ex, "", "Error ");
//            }
//        }
//
//        private void SolutionControl_OnSolutionSaved(object sender, BOEventArgs e)
//        {
//            try
//            {
//                DMSolution  dmSolution = (DMSolution) e.BusinessObject;
//                _solutionControlForm.Close();
//                if (dmSolution == null) return;
//                if (this.SolutionSaved != null)
//                {
//                    this.SolutionSaved(this, new BOEventArgs<DMSolution>(dmSolution));
//                }
//            }
//            catch (Exception ex)
//            {
//                GlobalRegistry.UIExceptionNotifier.Notify(ex, "", "Error ");
//            }
//        }
//
//        /// <summary>
//        ///             Sets the form to control
//        /// </summary>
//        /// <param name="form">The form to control</param>
//        public void SetForm(IFormHabanero form)
//        {
//            Form winForm = (Form)form;
//            winForm.ControlBox = false;
//            winForm.FormBorderStyle = FormBorderStyle.FixedDialog;
//        }
//
//       /* private PanelWin CreateNewOpenControl()
//        {
//            LinkLabel newLinkLabel = new LinkLabel
//            {
//                Width = 150,
//                Text = "Create New Solution",
//                Font = new Font("Arial", 9.75F),
//                LinkColor = Color.White,
//                BackColor = Color.Transparent,
//                Top = 150,
//                Left = 700
//            };
//
//            newLinkLabel.Click += NewLinkLabel_OnClick;
//
//
//
//            LinkLabel openLinkLabel = new LinkLabel
//            {
//                Width = 150,
//                Text = "Open Existing Solution",
//                Font = new Font("Arial", 9.75F),
//                LinkColor = Color.White,
//                BackColor = Color.Transparent,
//                Top = 200,
//                Left = newLinkLabel.Left
//            };
//
//            openLinkLabel.Click += OpenLinkLabel_OnClick;
//
//            PanelWin recentSolutionPanel = CreateRecentSolutionPanel();
//            recentSolutionPanel.BackColor = Color.Transparent;
//            recentSolutionPanel.Left = newLinkLabel.Left;
//            recentSolutionPanel.Top = 300;
//            PanelWin panel3 = new PanelWin { Height = 70, Width = 220 };
//
//            panel3.Controls.Add(newLinkLabel);
//
//            panel3.Controls.Add(openLinkLabel);
//            panel3.Controls.Add(recentSolutionPanel);
//            return panel3;
//        }*/
//
//        private GroupBoxWin CreateNewOpenControl()
//        {
//            Image image1 = FMResources.Load;
//            LabelWin newImageLabel = new LabelWin
//                                         {
//                                             Size = new Size(image1.Width, image1.Height),
//                                             Image = image1,
//                                             Top = 10,
//                                             Left = 100
//                                         };
//            LinkLabel newLinkLabel = new LinkLabel
//                                         {
//                                             Width = 150,
//                                             Text = "Create New Solution",
//                                             Font = new System.Drawing.Font("Arial", 9.75F),
//                                             Top = 10,
//                                             Left = newImageLabel.Left + newImageLabel.Width + 5
//                                         };
//            newLinkLabel.Click += NewLinkLabel_OnClick;
//            
//            LabelWin dbImageLabel = new LabelWin
//                                         {
//                                             Size = new Size(image1.Width, image1.Height),
//                                             Image = image1,
//                                             Top = 40,
//                                             Left = 100
//                                         };
//            LinkLabel dbLinkLabel = new LinkLabel
//                                        {
//                                            Width = 200,
//                                            Text = "Create Solution from Database",
//                                            Font = new System.Drawing.Font("Arial", 9.75F),
//                                            Top = dbImageLabel.Top,
//                                            Left = newImageLabel.Left + newImageLabel.Width + 5
//                                         };
//            dbLinkLabel.Click += DBLinkLabel_OnClick;
//            
//            LabelWin classdefsImageLabel = new LabelWin
//                                         {
//                                             Size = new Size(image1.Width, image1.Height),
//                                             Image = image1,
//                                             Top = 70,
//                                             Left = 100
//                                         };
//            LinkLabel classdefsLinkLabel = new LinkLabel
//                                        {
//                                            Width = 200,
//                                            Text = "Create Solution from ClassDefs",
//                                            Font = new System.Drawing.Font("Arial", 9.75F),
//                                            Top = classdefsImageLabel.Top,
//                                            Left = newImageLabel.Left + newImageLabel.Width + 5
//                                         };
//            classdefsLinkLabel.Click += ClassDefsLinkLabel_OnClick;
//
//            LabelWin openImageLabel = new LabelWin
//                                          {
//                                              Size = new Size(image1.Width, image1.Height),
//                                              Image = image1,
//                                              Top = 100,
//                                              Left = 100
//                                          };
//
//            LinkLabel openLinkLabel = new LinkLabel
//                                          {
//                                              Width = 150,
//                                              Text = "Open Existing Solution",
//                                              Font = new System.Drawing.Font("Arial", 9.75F),
//                                              Top = openImageLabel.Top,
//                                              Left = newImageLabel.Left + newImageLabel.Width + 5
//                                          };
//            openLinkLabel.Click += OpenLinkLabel_OnClick;
//            
//            Panel recentSolutionPanel = CreateRecentSolutionPanel();
//            recentSolutionPanel.BackColor = Color.Transparent;
//            recentSolutionPanel.Left = newLinkLabel.Left;
//            recentSolutionPanel.Top = openImageLabel.Top + 10;
//
//            GroupBoxWin panel3 = new GroupBoxWin {Height = 70, Width = 220};
//            panel3.Controls.Add(newImageLabel);
//            panel3.Controls.Add(newLinkLabel);
//            panel3.Controls.Add(dbImageLabel);
//            panel3.Controls.Add(dbLinkLabel);
//            panel3.Controls.Add(classdefsImageLabel);
//            panel3.Controls.Add(classdefsLinkLabel);
//            panel3.Controls.Add(openImageLabel);
//            panel3.Controls.Add(openLinkLabel);
//            panel3.Controls.Add(recentSolutionPanel);
//            return panel3;
//        }
//
//       
//        private void ClassDefsLinkLabel_OnClick(object sender, EventArgs e)
//        {
//            OpenFileDialog fileDialog = new OpenFileDialog();
//            if (fileDialog.ShowDialog() == DialogResult.OK)
//            {
//                SetupNewDataAccessor();
//                DMSolution dmSolution = new SolutionCreator().CreateSolution();
//                dmSolution.Assemblies.ForEach(assembly => assembly.MarkForDelete());
//                try
//                {
//                    XmlClassDefsLoader loader = new XmlClassDefsLoader("", new DtdLoader(),
//                                                                       new FMDefClassFactory(dmSolution));
//                    loader.LoadClassDefs(File.ReadAllText(fileDialog.FileName));
//                    dmSolution.DefaultAssembly = dmSolution.Assemblies.FirstOrDefault();
//                    DMSolutionPostLoadProcessor processor = new DMSolutionPostLoadProcessor(IOCContainer);
//                    processor.ProcessSolution(dmSolution);
//                }
//                catch (Exception ex)
//                {
//                    GlobalRegistry.UIExceptionNotifier.Notify(ex, "", "Error ");
//                }
//                dmSolution.Save();
////                Solution = dmSolution;
//            }
//        }
//
//        private void DBLinkLabel_OnClick(object sender, EventArgs e)
//        {
//            
//        }
//
//        private void SetupNewDataAccessor()
//        {
//            //BORegistry.DataAccessor = new NonValidatingDataAccessorInMemory();
//            _dataStoreInMemory = new DataStoreInMemory();
//            //BORegistry.DataAccessor = new DataAccessorInMemory(_dataStoreInMemory);
//            BORegistry.DataAccessor = new NonValidatingDataAccessorInMemory(_dataStoreInMemory);
//            BusinessObjectManager.Instance.ClearLoadedObjects();
//        }
//
////      
////        private IPanel CreateHeadingPanel()
////        {
////            LabelWin labelImage = new LabelWin();
////            //Image image = FMResources.LocalGov2;
////            Image image = FMResources.logo;
////            labelImage.Size = new Size(image.Width, image.Height);
////            labelImage.Image = image;
////            
////            IPanel panel2 = this.ControlFactory.CreatePanel();
////            panel2.Width = 400;
////            panel2.Height = 200;
////            ColumnLayoutManager columnLayoutManager = new ColumnLayoutManager(panel2, this.ControlFactory);
////            columnLayoutManager.ColumnCount = 1;
////            //ILabel label1 = this.ControlFactory.CreateLabel("Local Goverment Management Information System");
////            ILabel label1 = this.ControlFactory.CreateLabel("LGMIS");
////            label1.Font = new System.Drawing.Font("Arial", 14F, System.Drawing.FontStyle.Bold);
////            label1.ForeColor = Color.Blue;
////            ILabel label2 = this.ControlFactory.CreateLabel("Entity Definition Configurer");
////            label2.Font = new System.Drawing.Font("Arial", 9.75F, System.Drawing.FontStyle.Bold);
////            label2.TextAlign = ContentAlignment.MiddleLeft;
////            label2.ForeColor = Color.Blue;
////            columnLayoutManager.AddControl(label1);
////            columnLayoutManager.AddControl(label2);
////
////
////            IPanel panel1 = this.ControlFactory.CreatePanel();
////            panel1.Width = 600;
////            panel1.Height = image.Height + 5;
////            FlowLayoutManager flowLayoutManager = new FlowLayoutManager(panel1, this.ControlFactory);
////            flowLayoutManager.AddControl(labelImage);
////            flowLayoutManager.AddControl(panel2);
////            return panel1;
////        }
//    }
//
//    
//}